package com.share.tool.m2d.drds;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author syq
 * @data 2020/1/7
 */
public class MainDrdsBatch {
    private static String sqlInset = "INSERT INTO `bbt_jd_member` (`id`, `seller_nick`, `pin`, `mobile`, `fullname`) VALUES (545,'1','2','3','4'),(5415,'1','2','3','4'),(545,'1','2','3','4');";

    public static void main(String[] args) {
        /*java -jar bbt-data.jar sql skipColounIndexs skipDuplicateIndexs*/
        if (args.length != 4) {
            System.out.println("参数个数错误-->java -jar bbt-data.jar sql skipColounIndexs skipDuplicateIndexs");
            return;
        }

        String sql = args[0];
        String skipColounIndexs = args[1] + ','; /*0,*/
        String skipDuplicateIndexs = args[2] + ',';/*0,1,3,*/

//        String sql = sqlInset;
//        String skipColounIndexs = "0,";
//        String skipDuplicateIndexs = "0,1,3,";

        SqlStruct sqlStruct = AnalyzerInsertSql(sql);
        List<String> list = genInsertSql(sqlStruct, skipColounIndexs, skipDuplicateIndexs);
        if (list != null) {
            for (String tmp : list) {
                System.out.println(tmp);
            }
        }
    }

    /*sql凭借*/
    private static List<String> genInsertSql(SqlStruct sqlStru1, String skipColounIndexs, String skipDuplicateIndexs) {
        if (sqlStru1 == null || sqlStru1.getInsertPre() == null || sqlStru1.getColoun() == null || sqlStru1.getValues() == null) {
            return null;
        }
        List<String> reList = new LinkedList<>();

        String[] arrColoun = sqlStru1.coloun;
        List<String[]> values = sqlStru1.values;
        String beginStr = sqlStru1.insertPre;
        int len = sqlStru1.colounLen;

        for (String[] arrValue : values) {
            //insert
            StringBuffer reSb = new StringBuffer(beginStr);
            for (int i = 0; i < len; i++) {
                if (i == 0) {
                    reSb.append("(");
                }

                if (skipColounIndexs.contains(i + ",")) {
                    continue;
                }
                reSb.append(arrColoun[i] + ",");

                if (i == len - 1) {
                    int index = reSb.lastIndexOf(",");
                    if (index > 0) {
                        reSb.setCharAt(index, ')');
                    }
                }
            }

            //value
            reSb.append(" values ");
            for (int i = 0; i < len; i++) {
                if (i == 0) {
                    reSb.append("(");
                }

                if (skipColounIndexs.contains(i + ",")) {
                    continue;
                }
                reSb.append(arrValue[i] + ",");

                if (i == len - 1) {
                    int index = reSb.lastIndexOf(",");
                    if (index > 0) {
                        reSb.setCharAt(index, ')');
                    }
                }
            }

            //dump
            reSb.append(" ON DUPLICATE KEY UPDATE ");
            for (int i = 0; i < len; i++) {
                if (skipDuplicateIndexs.contains(i + ",")) {
                    continue;
                }
                reSb.append(arrColoun[i] + "=" + arrValue[i] + ",");

                if (i == len - 1) {
                    int index = reSb.lastIndexOf(",");
                    if (index > 0) {
                        reSb.setCharAt(index, ';');
                    }
                }
            }

            reList.add(reSb.toString());
        }

        return reList;
    }

    private static String sqlitChar = "#1@1#";

    private static SqlStruct AnalyzerInsertSql(String sql) {
        SqlStruct sqlStruct = new SqlStruct();
        int startColoun = sql.indexOf("(");
        if (startColoun < 5) {
            return null;
        }
        String beginStr = sql.substring(0, startColoun);
        sqlStruct.setInsertPre(beginStr);

        String colounSql = sql.replaceAll("\\) {0,2}VALUES {0,2}\\(", sqlitChar);
        int endColoun = colounSql.indexOf(sqlitChar);
        int endValue = colounSql.lastIndexOf(")");
        if (endColoun < 0 || endValue < endColoun) {
            return null;
        }
        String colounStr = sql.substring(startColoun + 1, endColoun);
        String[] coloun = colounStr.split(",");
        sqlStruct.setColoun(coloun);
        sqlStruct.setColounLen(coloun.length);

        String tmpValueSql = colounSql.substring(endColoun, endValue);
        tmpValueSql = tmpValueSql.replaceAll("\\) {0,2}, {0,2}\\(", sqlitChar);
        String[] tmpValueArr = tmpValueSql.split(sqlitChar);
        List<String[]> valueList = new ArrayList<>();
        for (String tmp : tmpValueArr) {
            if (tmp == null || "".equals(tmp.trim())) {
                continue;
            }
            String[] value = tmp.split(",");
            if (value == null || value.length != coloun.length) {
                continue;
            }
            valueList.add(value);
        }
        sqlStruct.setValues(valueList);
        return sqlStruct;
    }
}
